{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ded5553",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Start H2O-3 runtime.\n",
    "\n",
    "import h2o\n",
    "h2o.init(strict_version_check=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e746ad4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configure K-Means algorithm and train a model.\n",
    "\n",
    "from h2o.estimators import H2OKMeansEstimator\n",
    "\n",
    "# Import the iris dataset into H2O:\n",
    "iris = h2o.import_file(\"http://h2o-public-test-data.s3.amazonaws.com/smalldata/iris/iris_wheader.csv\")\n",
    "\n",
    "# Set the predictors:\n",
    "predictors = [\"sepal_len\", \"sepal_wid\", \"petal_len\", \"petal_wid\"]\n",
    "\n",
    "# Split the dataset into a train and valid set:\n",
    "train, valid = iris.split_frame(ratios=[.8], seed=1234)\n",
    "\n",
    "# Build and train the model:\n",
    "kmeans = H2OKMeansEstimator(k=10,\n",
    "                            estimate_k=True,\n",
    "                            standardize=False,\n",
    "                            seed=1234)\n",
    "kmeans.train(x=predictors,\n",
    "                  training_frame=train,\n",
    "                  validation_frame=valid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29eb0722",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Log the model to an MLFlow reqistry.\n",
    "\n",
    "import mlflow\n",
    "import h2o_mlflow_flavor\n",
    "mlflow.set_tracking_uri(\"http://127.0.0.1:8080\")\n",
    "\n",
    "with mlflow.start_run(run_name=\"iris\") as run:\n",
    "    mlflow.log_params(h2o_mlflow_flavor.get_params(kmeans)) # Log training parameters of the model (optional).\n",
    "    mlflow.log_metrics(h2o_mlflow_flavor.get_metrics(kmeans)) # Log performance matrics of the model (optional).\n",
    "    input_example = h2o_mlflow_flavor.get_input_example(kmeans) # Extract input example from training dataset (optional)\n",
    "    h2o_mlflow_flavor.log_model(kmeans, \"iris\", input_example=input_example,\n",
    "                                model_type=\"POJO\") # Specify whether the output model should be MOJO or POJO. (MOJO is default)\n",
    "                                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bed1dafe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load model from the MLFlow registry and score with the model.\n",
    "\n",
    "import mlflow\n",
    "mlflow.set_tracking_uri(\"http://127.0.0.1:8080\")\n",
    "\n",
    "logged_model = 'runs:/9a42265cf0ef484c905b02afb8fe6246/iris' # Specify correct id of your run.\n",
    "\n",
    "# Load model as a PyFuncModel.\n",
    "loaded_model = mlflow.pyfunc.load_model(logged_model)\n",
    "\n",
    "# Predict on a Pandas DataFrame.\n",
    "import pandas as pd\n",
    "data = pd.read_csv(\"http://h2o-public-test-data.s3.amazonaws.com/smalldata/iris/iris_wheader.csv\")\n",
    "loaded_model.predict(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "905b0c4c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mlflow",
   "language": "python",
   "name": "mlflow"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
